package com.zhoukai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhoukai.constants.Constants;
import com.zhoukai.dto.ProviderDto;
import com.zhoukai.mapper.ProviderMapper;
import com.zhoukai.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import com.zhoukai.domain.Provider;
import com.zhoukai.service.ProviderService;

/**
 * Created with IntelliJ IDEA.
 * @Author: zk
 * @Date: 2022/02/08/13:47
 * @Description: 
 */
@DubboService(methods = {@Method(name = "addProvider",retries = 0)})
public class ProviderServiceImpl implements ProviderService{

    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public DataGridView listProviderPage(ProviderDto providerDto) {
//        Page<Provider> page=new Page<>(providerDto.getPageNum(),providerDto.getPageSize());
//        QueryWrapper<Provider> qw=new QueryWrapper<>();
//        qw.like(StringUtils.isNotBlank(providerDto.getProviderName()),Provider.COL_PROVIDER_NAME,providerDto.getProviderName());
//        qw.like(StringUtils.isNotBlank(providerDto.getContactName()),Provider.COL_CONTACT_NAME,providerDto.getContactName());
//        qw.and(StringUtils.isNotBlank(providerDto.getContactTel()), new Consumer<QueryWrapper<Provider>>() {
//            @Override //(tel like ? or mobile like ?)
//            public void accept(QueryWrapper<Provider> providerQueryWrapper) {
//                providerQueryWrapper.like(Provider.COL_CONTACT_TEL,providerDto.getContactTel())
//                        .or().like(Provider.COL_CONTACT_MOBILE,providerDto.getContactTel());
//            }
//        });
//        qw.eq(StringUtils.isNotBlank(providerDto.getStatus()),Provider.COL_STATUS,providerDto.getStatus());
//        this.providerMapper.selectPage(page,qw);
//
        Page<Provider> page=new Page<>(providerDto.getPageNum(),providerDto.getPageSize());
        this.providerMapper.selectPage(page, Wrappers.lambdaQuery(Provider.class)
                .like(StringUtils.isNotBlank(providerDto.getProviderName()),Provider::getProviderName,providerDto.getProviderName())
                .like(StringUtils.isNotBlank(providerDto.getContactName()), Provider::getContactName,providerDto.getContactName())
                .and(StringUtils.isNotBlank(providerDto.getContactTel()), new Consumer<LambdaQueryWrapper<Provider>>() {
                    @Override
                    public void accept(LambdaQueryWrapper<Provider> providerLambdaQueryWrapper) {
                        providerLambdaQueryWrapper.like(Provider::getContactTel,providerDto.getContactTel())
                                .or().like(Provider::getContactMobile,providerDto.getContactTel());
                    }
                }).eq(StringUtils.isNotBlank(providerDto.getStatus()),Provider::getStatus,providerDto.getStatus())
        );
        return new DataGridView(page.getTotal(),page.getRecords());
    }


    @Override
    public Provider getOne(Long providerId) {
        return this.providerMapper.selectById(providerId);
    }

    @Override
    public int addProvider(ProviderDto providerDto) {
        Provider provider=new Provider();
        BeanUtil.copyProperties(providerDto,provider);
        provider.setCreateTime(DateUtil.date());
        provider.setCreateBy(providerDto.getSimpleUser().getUserName());
        return this.providerMapper.insert(provider);

    }

    @Override
    public int updateProvider(ProviderDto providerDto) {
        Provider provider = new Provider();
        BeanUtil.copyProperties(providerDto,provider);
        provider.setUpdateBy(providerDto.getSimpleUser().getUserName());
        return this.providerMapper.updateById(provider);
    }

    @Override
    public int deleteProviderByIds(Long[] providerIds) {
        List<Long> ids = Arrays.asList(providerIds);
        if(ids.size()>0){
            return this.providerMapper.deleteBatchIds(ids);
        }

        return 0;
    }

    @Override
    public List<Provider> selectAllProvider() {
        return this.providerMapper.selectList(Wrappers.lambdaQuery(Provider.class)
            .eq(Provider::getStatus,Constants.STATUS_TRUE));
    }
}
